<template>
  <div class="addr-change-win">
    <el-dialog
      title="更改目标地址"
      :visible.sync="visible"
      :close-on-click-modal="false"
      width="40%"
      size="small"
      :before-close="onOk"
      @open="handleOpen"
    >
      <el-form :model="editAddressFormData" ref="editAddressForm" label-width="100px">
        <el-form-item label="原地址" prop="oldAddress">
          <div style="font-size:12px;" v-for="(item, i) in names" :key="i">{{ item }}</div>
        </el-form-item>
        <el-form-item label="目标地址" prop="targetAddress" id="loadMoreSelect2">
          <el-select
            v-model="editAddressFormData.targetAddress"
            placeholder="选择新的目标地址"
            size="mini"
            :filter-method="searchNewTargetAddr"
            filterable
            clearable
            @clear="selectClear"
          >
            <el-option
              v-for="(e, index) in targetOptions"
              :key="index"
              :label="e.target_address"
              :value="e.target_address"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="margin-top: 200px;">
          <el-button size="mini" type="primary" @click="confirmEditAddress">确定</el-button>
          <el-button size="mini" @click="onOk">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addOperationLog } from "@/utils/index";

export default {
  props: {
    visible: Boolean,
    onOk: Function,
    confirmSubmit: Function,
    names: Array,
    tarAddrOptions: Array,
  },
  data() {
    return {
      editAddressFormData: {
        oldAddress: "",
        targetAddress: "",
      },
      currentPage: 1,
      isExpend: true,
      timer: null,
      newTarAddrOptions: [],
      editIds: []
      // query: 0,
    };
  },
  computed: {
    // 仅读取
    targetOptions: function () {
      return JSON.parse(JSON.stringify(this.newTarAddrOptions)).splice(
        0,
        this.currentPage * 10
      );
    },
  },
  watch: {
    tarAddrOptions: function (newval) {
      this.newTarAddrOptions = newval
    },
  },
  methods: {
    index() {
      // target_address.onOk();
      this.editAddressFormData = {
        oldAddress: "",
        targetAddress: "",
      };
    },
    async confirmEditAddress() {
      if (!this.editAddressFormData.targetAddress) {
        this.$message.warning("请选择需要更改的地址");
        return;
      }
      const res = await this.axios.post(
        this.apis.addressDataUpdateTargetAddress,
        {
          ids: this.editIds,
          import_file_record_id: Number(this.$store.state.currentFileInfo.id),
          new_target_address: this.editAddressFormData.targetAddress,
        }
      );
      if (res.retCode === 2000) {
        this.$message.success("更改目标地址成功!");
        this.onOk();
        await addOperationLog({
          title: "更改目标地址",
          content: {
            old_target_address: this.names,
            new_target_address: this.editAddressFormData.targetAddress,
          },
        });
      }
    },
    searchNewTargetAddr(val) {
      if (this.timer) {
        clearTimeout(this.timer);
      }
      this.timer = setTimeout(async () => {
        const res = await this.axios.post(
          this.apis.addressDataGroupingStatistics,
          {
            target_address: val,
            import_file_record_id: this.$store.state.currentFileInfo.id,
          }
        );
        if (res.retCode === 2000) {
          console.log(this.newTarAddrOptions, "this.newTarAddrOptions");
          this.newTarAddrOptions = res.rows.items;
        }
      }, 800);
    },
    selectClear(){
      this.searchNewTargetAddr()
      // 当搜索了一个数据并且选中，再次点击清除，需要重新发起请求，并且吧滚动监听再次加上。
      // this.setLoadMore()
    },
    handleOpen() {
      this.$nextTick(() => {
        this.setLoadMore();
      });
    },
    //给select框加滚动监测
    setLoadMore() {
      const that = this;
      const loadSelectElement = document
        .getElementById("loadMoreSelect2")
        .getElementsByClassName("el-select-dropdown__wrap")[0];
        console.log(loadSelectElement, 'loadSelectElement');
      let timer = null;
      function scrollLoadData() {
        if (timer) {
          clearTimeout(timer);
        }
        timer = setTimeout(() => {
          const scrollDistance =
            loadSelectElement.scrollHeight -
            loadSelectElement.scrollTop -
            loadSelectElement.clientHeight;
          // 非搜索
          console.log(scrollDistance, 'scrollDistance');
          if (scrollDistance <= 0 && !that.query) {
            console.log("触底了");
            //页码依次加1
            if (
              JSON.parse(JSON.stringify(that.newTarAddrOptions)).splice(
                that.currentPage * 10,
                10
              ).length
            ) {
              console.log("有东西可以加一");
              that.currentPage++;
            }
          }
        }, 300);
      }
      loadSelectElement.addEventListener("scroll", scrollLoadData);
      //如果已加载完全部数据，取消监听
      if (that.getAll) {
        loadSelectElement.removeEventListener("scroll", scrollLoadData);
      }
    },
  },
  mounted() {
    // this.$nextTick(() => {
    //   this.setLoadMore()
    // }, 3000)
    // setTimeout(() => {
    //   console.log(document.getElementById('loadMoreSelect2'), 'xx');
    // }, 3000);
  },
};
</script>

<style></style>
